package service;

import pojo.User;
import verify.MyVerify;
import dao.UserDao;
import dao.impl.UserDaoImpl;

public class RegisterServiceImpl {
	
	private MyVerify myVerify;
	private UserDao userDao;
	
	public RegisterServiceImpl(){
		myVerify = new MyVerify();
		userDao = new UserDaoImpl();
	}
	
	public String verify(String uname,String sal,String birth,String sex){
		String verify = myVerify.verifyString(uname);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "uname"+verify;
		}
		verify = myVerify.verifyDouble(sal);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "sal"+verify;
		}
		
		verify = myVerify.verifyDate(birth);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "birth"+verify;
		}
		verify = myVerify.verifyBoolean(sex);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "sex"+verify;
		}
		return myVerify.SUCCESS;
		
	}
	
	public String verify2(String uname,String sal,String birth,String sex,String avatar){
		String verify = myVerify.verifyString(uname);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "uname"+verify;
		}
		verify = myVerify.verifyDouble(sal);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "sal"+verify;
		}
		
		verify = myVerify.verifyDate(birth);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "birth"+verify;
		}
		verify = myVerify.verifyBoolean(sex);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "sex"+verify;
		}
		
		verify = myVerify.verifyString(avatar);
		if (!MyVerify.SUCCESS.equals(verify)) {
			return "avatar"+verify;
		}
		return myVerify.SUCCESS;
		
	}
	
	public String register(User user){
		User u = userDao.findByName(user.getUname());
		if (u==null) {
			int result = userDao.addUser(user);
			if (result==0) {
				return "insert fail";
			}
			return "success";
		}else{
			return "user already exist";
		}
		
		
	}

}
